package org.lynn.javax.demo.jframe;

import lombok.Getter;
import org.lynn.javax.demo.imooc.AlgoVisHelper;

import javax.swing.*;
import java.awt.*;

/**
 * @author linzou@linkdoc.com
 * @ClassName: vAlgFrame
 * @Description:
 * @date 2019/6/18 8:40 AM
 */
@Getter
public class ValgFrame extends JFrame {

    private int canvasWidth;
    private int canvasHeight;
    private Circle[] circles;
    private JPanel canvas;


    public ValgFrame(String title,int canvasWidth,int canvasHeight){
        super(title);

        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        ValgCanvas canvas = new ValgCanvas();
        setContentPane(canvas);
        pack();
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        setVisible(true);
    }


    public ValgFrame(String title){
        this(title,1024,768);
    }

    public int getCanvasWidth() {
        return canvasWidth;
    }

    public int getCanvasHeight() {
        return canvasHeight;
    }

    public void render(Circle[] circles){
        this.circles = circles;
        repaint();
    }





    private class ValgCanvas extends JPanel{

        public ValgCanvas(){
            //双缓存
            super(true);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D)g;

            // 抗锯齿
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.addRenderingHints(hints);

            // 具体绘制
            AlgoVisHelper.setStrokeWidth(g2d, 1);
            AlgoVisHelper.setColor(g2d, Color.RED);
            for (Circle circle : circles) {
                AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
            }
        }

        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth,canvasHeight);
        }

    }
}
